var wxcard = {
    init: function(merchantId) {
        this.merchantId = merchantId;

        $.post("/initWx", {"merchantid": merchantId}, function(data) {
            wx.config({
                debug: false,
                appId: data.appId,
                timestamp: data.timestamp,
                nonceStr: data.nonceStr,
                signature: data.signature,
                jsApiList: ['chooseCard']
            });
        });
    },
    list: function() {
        $.post("/chooseCard", {"data": "content"}, function(data) {
            wx.chooseCard({
                shopId: data.shopId,
                cardType: data.cardType,
                cardId: data.cardId,
                timestamp: data.timestamp,
                nonceStr: data.nonceStr,
                signType: data.signType,
                cardSign: data.cardSign,
                success: wxcard.updatedSelectedCoupon
            });
        });
    },
    updatedSelectedCoupon: function(res) {
        var actual_amount = document.getElementById("order-price").innerHTML;
        $.post("/codeDecrypt", {"pay": actual_amount, "context": res.cardList}, function(data) {
            var cardId = data.cardId;
            var code = data.code;
            var amount = data.amount;
            var priceElm = document.getElementById('price');
            if (priceElm) {
                priceElm.innerHTML = amount;
            }
            document.getElementById('coupon-label').innerHTML = data.show;
            document.getElementById('coupon-code').value = code;
            document.getElementById('card-id').value = cardId;
            document.getElementById('coupon-amount').value = amount;
        });
    }
}